#include "mlExtras.h"
#include <math.h>
#include <stdio.h>
#include <string.h>

const float
mlValuePi = 3.14159265358979323846f;

float
mlRadToDeg(float r)
{
	return r * (180.0f / mlValuePi);
}

float
mlDegToRad(float r)
{
	return r * (mlValuePi / 180.0f);
}

unsigned int
mlNextTwoPower(float x)
{
	return (unsigned int) powf(2.0f, ceilf(logf(x) / logf(2.0f)));
}

float
mlClamp(float x, float a, float b)
{
	return x < a ? a : (b < x ? b : x);
}

float
mlMax(float a, float b)
{
	return a > b ? a : b;
}

void
mlQuadBezier(const float *p0, const float *p1, const float *p2, float t, float *v) 
{
	v[0] = (1.0f - t) * (1.0f - t) * p0[0] + 2.0f * (1.0f - t) * t * p1[0] + t * t * p2[0];
	v[1] = (1.0f - t) * (1.0f - t) * p0[1] + 2.0f * (1.0f - t) * t * p1[1] + t * t * p2[1];
	v[2] = (1.0f - t) * (1.0f - t) * p0[2] + 2.0f * (1.0f - t) * t * p1[2] + t * t * p2[2];
}

void
mlCubicBezier(const float *p0, const float *p1, const float *p2, const float *p3, float t, float *v) 
{
	v[0] = (1.0f - t) * (1.0f - t) *(1.0f - t) * p0[0] + 3.0f * (1.0f - t) * (1.0f - t) * t * p1[0] + 3.0f * (1.0f - t) * t * t * p2[0] + t * t * t * p3[0];
	v[1] = (1.0f - t) * (1.0f - t) *(1.0f - t) * p0[1] + 3.0f * (1.0f - t) * (1.0f - t) * t * p1[1] + 3.0f * (1.0f - t) * t * t * p2[1] + t * t * t * p3[1];
	v[2] = (1.0f - t) * (1.0f - t) *(1.0f - t) * p0[2] + 3.0f * (1.0f - t) * (1.0f - t) * t * p1[2] + 3.0f * (1.0f - t) * t * t * p2[2] + t * t * t * p3[2];
}
